﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using XuanXiang.Infrastructure.Attr;
using XuanXiang.Infrastructure.Dto.Request;
using XuanXiang.Infrastructure.Dto.Request.System;
using XuanXiang.Infrastructure.Dto.Response.System;
using XuanXiang.Service.Interface;

namespace XuanXiang.Api.Admin.Controllers
{
    [ApiGroup("Admin(管理员模块)")]
    [Authorize(Roles = "admin")]
    public class SysController : BaseController
    {
        private readonly I_System_Service _system_Service;

        public SysController(I_Login_Service login_Service, I_System_Service system_Service) : base(login_Service)
        {
            _system_Service = system_Service;
        }

        /// <summary>
        /// 获取所有的字典
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Right_(urls = "/dictionary")]
        public async Task<IActionResult> Get_Dictionarys(string? key = "")
        {
            var result = await _system_Service.Get_Dictionarys(key ?? "");
            return Ok(result);
        }


        /// <summary>
        /// 获取字典下的子级
        /// </summary>
        /// <returns></returns>

        private async Task<IActionResult> Get_Dictionarys_Children(string? id, string? key = "")
        {
            var result = await _system_Service.Get_Dictionarys_Children(id, key ?? "");
            return Ok(result);
        }


        /// <summary>
        /// 修改字典的禁用/启用{id/isBan}
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Right_(urls = "/dictionary")]
        public async Task<IActionResult> Update_Dictionary_Ban(IsBan_Dto dto) {
            var result = await _system_Service.Update_Dictionary_Ban(dto);
            return Ok(result);
        }

        /// <summary>
        /// 新增字典
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Right_(urls = "/dictionary")]
        public async Task<IActionResult> Add_Dictionary(Dictionary_Request_dto dto) {
            var result = await _system_Service.Add_Dictionary(dto);
            return Ok(result);
        }

        /// <summary>
        /// 删除字典{id}
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Right_(urls = "/dictionary")]
        public async Task<IActionResult> Delete_Dictionary(Delete_Dto dto) {
            var result = await _system_Service.Delete_Dictionary(dto);
            return Ok(result);
        }

        /// <summary>
        /// 获取字典详情(商品类型)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get_Single_GoodsType_Dictionary(string id,int page=1,int limit=5) {
            var result = await _system_Service.Get_Single_GoodsType_Dictionary(new Fenye_Dto() { 
            page=page,
            limit=limit,
            id=id
            });
                return Ok(result);
        }

        /// <summary>
        /// 获取用户的菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Right_(urls = "/")]
        public async Task<IActionResult> Get_Users_Right() {
            var user = this.Get_User();
            var result = await _system_Service.Get_Users_Right(user.id);
            return Ok(result);
        }


        /// <summary>
        /// 获取所有的菜单权限
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Right_(urls = "/right,/right/give")]
        public async Task<IActionResult> Get_Right(string? id) 
        {
            var result = await _system_Service.Get_Right(id);
            return Ok(result);
        }


        /// <summary>
        /// 修改菜单
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Right_(urls = "/right,/right/give")]
        public async Task<IActionResult> Update_Right(Update_Right_Dto dto) {
            var reault = await _system_Service.Update_Right(dto);
            return Ok(reault);
        }


        /// <summary>
        /// 新增权限
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Right_(urls = "/right,/right/give")]
        public async Task<IActionResult> Add_Right(Add_Right_Dto dto) {
            var result = await _system_Service.Add_Right(dto);
            return Ok(result);
        }

        /// <summary>
        /// 删除权限
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Right_(urls = "/right,/right/give")]
        public async Task<IActionResult> Delete_Right(Delete_Dto dto) {
            var result = await _system_Service.Delete_Right(dto);
            return Ok(result);
        }

        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="key"></param>
        /// <returns></returns>
      /*  [HttpGet]
        public async Task<IActionResult> Get_Users(int page = 1, int limit = 10, string? key ="") {
            var result =await _system_Service.Get_Users(new Fenye_Dto() { 
            page=page,
            limit=limit,
            key=key??""
            });
            return Ok(result);
        
        }*/


        /// <summary>
        /// 用户授权{id,rights}
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Right_(urls = "/right,/right/give")]
        public async Task<IActionResult> Give_Users_Right(Right_dto dto) {
            var result = await _system_Service.Give_Users_Right(dto);
            return Ok(result);
        }





        //=============部门==================


        /// <summary>
        /// 获取所有部门
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Right_(urls = "/org,/org/department,/org/post")]
        public async Task<IActionResult> Get_Dept() {
            var result =await _system_Service.Get_Dept();
            return Ok(result);
        }


        /// <summary>
        /// 获取部门下的用户
        /// </summary>
        /// <param name="id"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpGet]
        [Right_(urls = "/org,/org/department,/org/post")]
        public async Task<IActionResult> Get_Dept_User(string? id, int page=1,int limit=10,string? key="") {
            var result = await _system_Service.Get_Dept_User(new Fenye_Dto() { 
                id=id,
            page=page,
            limit=limit,
            key=key??""
            });
            return Ok(result);
        }


        /// <summary>
        /// 新增部门
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost]
        [Right_(urls = "/org,/org/department,/org/post")]
        public async Task<IActionResult> Add_Dept(Add_Dept_Dto dto) {
            var result = await _system_Service.Add_Dept(dto);
            return Ok(result);
        }


        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost]
        [Right_(urls = "/org,/org/department,/org/post")]
        public async Task<IActionResult> Del_Dept(Delete_Dto dto) {
            var result = await _system_Service.Del_Dept(dto.id);
            return Ok(result);
        }

        /// <summary>
        /// 用户的禁用/启用{id,isBan}
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Right_(urls = "/org,/org/department,/org/post")]
        public async Task<IActionResult> IsBan_Users(IsBan_Dto dto) {
            var result = await _system_Service.IsBan_Users(dto);
            return Ok(result);
        }

        /// <summary>
        /// 删除部门用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [Right_(urls = "/org,/org/department,/org/post")]
        public async Task<IActionResult> Del_User(Delete_Dto dto) {
            var result = await _system_Service.Del_User(dto.id);
                return Ok(result);
        }


        /// <summary>
        /// 修改用户信息{id,name,account}
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Right_(urls = "/org,/org/department,/org/post")]
        public async Task<IActionResult> Update_Users(Users_Dto dto) {
            var result = await _system_Service.Update_Users(dto);
             return Ok(result);
        }

        /// <summary>
        /// 获取部门下的岗位
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Right_(urls = "/org,/org/department,/org/post")]
        public async Task<IActionResult> Get_Dept_Post(string id) {
            var result =await _system_Service.Get_Dept_Post(id);
            return Ok(result);
        }


        /// <summary>
        /// 新增部门下的用户
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Right_(urls = "/org,/org/department,/org/post")]
        public async Task<IActionResult> Add_User(Add_User_Dto dto) {
            var result = await _system_Service.Add_User(dto);
            return Ok(result);
        }

        /// <summary>
        /// 获取所有岗位
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        [Right_(urls = "/org,/org/department,/org/post")]
        public async Task<IActionResult> Get_Post(int page = 1, int limit = 10, string? key="") {
            var result = await _system_Service.Get_Post(new Fenye_Dto() { 
            page=page,
            limit=limit,
            key=key??""
            });
            return Ok(result);
        }

        /// <summary>
        /// 获取单个岗位
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        //[Right_(urls = "/org,/org/department,/org/post")]
        public async Task<IActionResult> Get_Single_Post(string? id)
        {
            var result = await _system_Service.Get_Single_Post(id);
            return Ok(result);
        }


        /// <summary>
        /// 获取单个岗位下的所有用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get_Single_Post_Users(string? id,int page=1,int limit=10) {
            var result = await _system_Service.Get_Single_Post_Users(new Fenye_Dto() { 
            id=id,
            page=page,
            limit=limit
            });
            return Ok(result);
        }


        /// <summary>
        /// 修改岗位
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<IActionResult> Update_Post(Update_Post_Dto dto)
        {
            var result = await _system_Service.Update_Post(dto);
            return Ok(result);
        }


        /// <summary>
        /// 新增岗位
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Add_Post(Add_Post_Dto dto) {
            var result = await _system_Service.Add_Post(dto);
            return Ok(result);
        }

        /// <summary>
        /// 删除岗位{id}
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Delete_Post(Delete_Dto dto) {
            var result = await _system_Service.Delete_Post(dto.id);
            return Ok(result);
        }
    }

}
